package com.sinosoft.dsp.component.bpm.util;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.faces.bean.ApplicationScoped;
import javax.faces.bean.ManagedBean;

import org.jbpm.api.Configuration;
import org.jbpm.api.ExecutionService;
import org.jbpm.api.HistoryService;
import org.jbpm.api.ProcessEngine;
import org.jbpm.api.RepositoryService;
import org.jbpm.api.TaskService;

/**
 * 流程引擎及其服务的管理
 * 
 * @author lichanghao
 * 
 */
@ManagedBean(name = "jbpmConfig")
@ApplicationScoped
public class JbpmConfig {

	private ProcessEngine processEngine = null;
	private RepositoryService repositoryService = null;
	private ExecutionService executionService = null;
	private HistoryService historyService = null;
	private TaskService taskService = null;

	/**
	 * 流程引擎的初始化方法 包括资源服务 执行服务 历史服务 任务服务的 初始化
	 * 
	 */
	@PostConstruct
	public void init() {
		// 启动工作流服务引擎，初始化ProcessEngine，自动读取jbpm.cfg.xml
		this.processEngine = new Configuration().buildProcessEngine();

		// 流程资源服务
		this.repositoryService = processEngine.getRepositoryService();

		// 流程执行服务
		this.executionService = processEngine.getExecutionService();

		// 流程任务服务
		this.taskService = processEngine.getTaskService();

		// 流程历史服务
		this.historyService = processEngine.getHistoryService();
	}

	public ProcessEngine getProcessEngine() {
		return processEngine;
	}

	public void setProcessEngine(ProcessEngine processEngine) {
		this.processEngine = processEngine;
	}

	public RepositoryService getRepositoryService() {
		return repositoryService;
	}

	public void setRepositoryService(RepositoryService repositoryService) {
		this.repositoryService = repositoryService;
	}

	public ExecutionService getExecutionService() {
		return executionService;
	}

	public void setExecutionService(ExecutionService executionService) {
		this.executionService = executionService;
	}

	public HistoryService getHistoryService() {
		return historyService;
	}

	public void setHistoryService(HistoryService historyService) {
		this.historyService = historyService;
	}

	public TaskService getTaskService() {
		return taskService;
	}

	public void setTaskService(TaskService taskService) {
		this.taskService = taskService;
	}

	@PreDestroy
	public void shutdown() {
		if (processEngine != null) {
			processEngine.close();
		}
	}

}
